﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ECRMS.WinForm.BaseClass;
using System.Linq;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.Repository;
using ECRMS.WinForm.BaseClass;
using ECRMS.WinForm.BaseClass.Common;
using ECRMS.WinForm.Properties;
using ECRMS.WinForm.WinService;
using ECRMS.WinForm.ModuleEdits;
using System.Linq;
using ECRMS.WinForm.BaseClass.Other;
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Nodes;
using DevExpress.XtraGrid.Views.Base;

namespace ECRMS.WinForm.Modules
{
    [ModuleInstance((int)ModuleEnum.指标测评)]
    public partial class TargetEvaluateModule : BaseModule
    {

        public int rowHandler;

        public bool IsModified = false;//默认没有被修改过

        public Guid ModifyIndexGroupId;//当前在编辑的指标组id

        public TargetEvaluateModule()
        {
            base.core.CreateWaitDialog("初始化中...","指标测评");
            InitializeComponent();
            InitView();
            InitData();
            InitEventHandler();
            //InitCombox();
            base.core.CloseWaitDialog();
        }

        private void InitCombox()
        {
            ComboBoxItemCollection col1 = rcbTureOFalse.Properties.Items;
            ComboBoxItemCollection col2 = rcbOneToNine.Properties.Items;
            ComboBoxItemCollection col3 = rcbZeroToNine.Properties.Items;
            col1.BeginUpdate();
            col2.BeginUpdate();
            col3.BeginUpdate();
            col1.Clear();
            col2.Clear();
            col3.Clear();

            col1.Add(new ComboxData { Text = "", FloatVaule = 0 });
            col1.Add(new ComboxData { Text = "0", FloatVaule = 0 });
            col1.Add(new ComboxData { Text = "1", FloatVaule = 0 });

            col2.Add(new ComboxData { Text = "", FloatVaule = 0 });
            col2.Add(new ComboxData { Text = "1", FloatVaule = 0 });
            col2.Add(new ComboxData { Text = "2", FloatVaule = 0 });
            col2.Add(new ComboxData { Text = "3", FloatVaule = 0 });
            col2.Add(new ComboxData { Text = "4", FloatVaule = 0 });
            col2.Add(new ComboxData { Text = "5", FloatVaule = 0 });
            col2.Add(new ComboxData { Text = "6", FloatVaule = 0 });
            col2.Add(new ComboxData { Text = "7", FloatVaule = 0 });
            col2.Add(new ComboxData { Text = "8", FloatVaule = 0 });
            col2.Add(new ComboxData { Text = "9", FloatVaule = 0 });


            col3.Add(new ComboxData { Text = "", FloatVaule = 0 });
            col3.Add(new ComboxData { Text = "0", FloatVaule = 0 });
            col3.Add(new ComboxData { Text = "1", FloatVaule = 0 });
            col3.Add(new ComboxData { Text = "2", FloatVaule = 0 });
            col3.Add(new ComboxData { Text = "3", FloatVaule = 0 });
            col3.Add(new ComboxData { Text = "4", FloatVaule = 0 });
            col3.Add(new ComboxData { Text = "5", FloatVaule = 0 });
            col3.Add(new ComboxData { Text = "6", FloatVaule = 0 });
            col3.Add(new ComboxData { Text = "7", FloatVaule = 0 });
            col3.Add(new ComboxData { Text = "8", FloatVaule = 0 });
            col3.Add(new ComboxData { Text = "9", FloatVaule = 0 });

            col1.EndUpdate();
            col2.EndUpdate();
            col3.EndUpdate();
        }

        private void InitEventHandler()
        {
            //语言切换
            base.core.LangChanged += new Core.LanguageChangedHandler(this.ChangeLanguage);
        }

        private void InitView()
        {
            base.newBarButtonVisiable = false;
            base.updateBarButtonVisiable = false;
            base.delBarButtonVisiable = false;
            base.selectBarButtonVisiable = false;
            base.otherBarButton2Visiable = false;
            base.otherBarButton3Visiable = false;
            base.outBarButtonVisiable = false;

            if (core.CoreUser.UserInfo.UserTypeId == (int)AuthorityEnum.CorporationClientAdmin)
            {
                base.other1BarButton1ImgaeIndex = 25;
                base.other1BarButton1Content = "测评";
                base.otherBarButton1Visiable = true;
                this.gdScore.Visible = true;
            }
            else
            {
                base.otherBarButton1Visiable = false;
                this.gdScore.Visible = false;
            }
        }

        private void InitData(bool isPostBack = false)
        {
            if (!isPostBack)
            {
                FuncResult result = core.CoreIndexEvaluate.GetIndexGroups(core.CoreUser.UserInfo.CorporationRefId,core.CoreUser.UserInfo.UserTypeId);
                if (result.Status == 0)
                {
                    XtraMessageBox.Show(this, Language_ch.ResourceManager.GetString(result.Message));
                    return;
                }
            }

            Guid rowIndex = core.CoreCommon.GetGridFocusRowRefId(gdindexgroup, gridView2, "CorpIndexGroupRefId");
            this.ModifyIndexGroupId = rowIndex;

            this.gdindexgroup.DataSource = core.CoreIndexEvaluate.IndexGroups;
            this.gdindexgroup.RefreshDataSource();
            this.gdindexgroup.Select();
            BindIndexs();
        }

        private void BindIndexs(bool isPostBack=false)
        {
            Guid rowIndex = core.CoreCommon.GetGridFocusRowRefId(gdindexgroup, gridView2, "CorpIndexGroupRefId");
            if (!isPostBack)
            { 
                
                FuncResult result = core.CoreIndexEvaluate.GetIndexs(rowIndex,core.CoreUser.UserInfo.UserRefId,core.CoreUser.UserInfo.UserTypeId);
                if (result.Status == 0)
                {
                    XtraMessageBox.Show(this, Language_ch.ResourceManager.GetString(result.Message));
                    return;
                }
            }
            int maxLevel = core.CoreIndexEvaluate.GetGroupMaxLevel();
            for (int i = 1; i <= maxLevel; i++)
            {
                switch (i)
                {
                    case 1:
                        this.gc1.Visible = true;
                        this.gc1.VisibleIndex = 1;
                        if (i == maxLevel)
                        {
                            this.gc1.Width = 300;
                        }
                        else
                        {
                            this.gc1.Width = 80;
                        }
                        break;
                    case 2:
                        this.gc2.Visible = true;
                        this.gc2.VisibleIndex = 2;
                        if (i == maxLevel)
                        {
                            this.gc2.Width = 300;
                        }
                        else
                        {
                            this.gc2.Width = 80;
                        }
                        break;
                    case 3:
                        this.gc3.Visible = true;
                        this.gc3.VisibleIndex = 3;
                        if (i == maxLevel)
                        {
                            this.gc3.Width = 300;
                        }
                        else
                        {
                            this.gc3.Width = 80;
                        }
                        break;
                    case 4:
                        this.gc4.Visible = true;
                        this.gc4.VisibleIndex = 4;
                        if (i == maxLevel)
                        {
                            this.gc4.Width = 300;
                        }
                        else
                        {
                            this.gc4.Width = 80;
                        }
                        break;
                    case 5:
                        this.gc5.Visible = true;
                        this.gc5.VisibleIndex = 5;
                        if (i == maxLevel)
                        {
                            this.gc5.Width = 300;
                        }
                        else
                        {
                            this.gc5.Width = 80;
                        }
                        break;
                    case 6:
                        this.gc6.Visible = true;
                        this.gc6.VisibleIndex = 6;
                        if (i == maxLevel)
                        {
                            this.gc6.Width = 300;
                        }
                        else
                        {
                            this.gc6.Width = 80;
                        }
                        break;
                    case 7:
                        this.gc7.Visible = true;
                        this.gc7.VisibleIndex = 7;
                        if (i == maxLevel)
                        {
                            this.gc7.Width = 300;
                        }
                        else
                        {
                            this.gc7.Width = 80;
                        }
                        break;
                    case 8:
                        this.gc8.Visible = true;
                        this.gc8.VisibleIndex = 8;
                        if (i == maxLevel)
                        {
                            this.gc8.Width = 300;
                        }
                        else
                        {
                            this.gc8.Width = 80;
                        }
                        break;
                    case 9:
                        this.gc9.Visible = true;
                        this.gc9.VisibleIndex = 9;
                        if (i == maxLevel)
                        {
                            this.gc9.Width = 300;
                        }
                        else
                        {
                            this.gc9.Width = 80;
                        }
                        break;
                    default:
                        break;
                }
            }
            this.gcNote.VisibleIndex = maxLevel + 1;
            this.gcNote.Width = 120;
            this.gcPoint.VisibleIndex = maxLevel + 2;
            this.gcPoint.Width = 40;
            gdIndexs.DataSource = core.CoreIndexEvaluate.OperatorRowIndexs;
            gdIndexs.RefreshDataSource();

        }

        private void ChangeLanguage(LangEnum lang)
        {
            //switch (core.NowLanguage)
            switch (lang)
            {
                case LangEnum.China:
                case LangEnum.中文:

                    break;
                case LangEnum.English:
                case LangEnum.英文:

                    break;
                default:
                    break;
            }
        }

        private void gridView2_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {

            Guid currentRowIndex = core.CoreCommon.GetGridFocusRowRefId(gdindexgroup, gridView1, "CorpIndexGroupRefId");
            if (currentRowIndex != ModifyIndexGroupId && IsModified)//切换指标组的之后如果存在原先指标组的数据已经修改过
            {
                DialogResult result = XtraMessageBox.Show(this, "指标组已有修改，是否保存修改的数据？", "保存数据", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)//确定保存
                {
                    FuncResult result1 = core.CoreIndexEvaluate.UpdateIndexs();
                    if (result1.Status == 0)
                    {
                        XtraMessageBox.Show(this, Language_ch.ResourceManager.GetString(result1.Message));
                        return;
                    }
                    IsModified = false;
                    ModifyIndexGroupId = currentRowIndex;
                    BindIndexs();
                }
                else if (result == DialogResult.No)
                {
                    IsModified = false;
                    ModifyIndexGroupId = currentRowIndex;
                    BindIndexs();
                }
            }
            else
            {
                IsModified = false;
                ModifyIndexGroupId = currentRowIndex;
                BindIndexs();
            };
        }

        public override void saveBarButton_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            base.saveBarButton_ItemClick(sender, e);

            DialogResult result = XtraMessageBox.Show(this, "确定要提交已经测评了的数据吗？", "提交数据", MessageBoxButtons.OKCancel);
            if (result == DialogResult.OK)//确定保存
            {
                FuncResult result1 = core.CoreIndexEvaluate.UpdateIndexs();
                if (result1.Status == 0)
                {
                    XtraMessageBox.Show(this, Language_ch.ResourceManager.GetString(result1.Message));
                    return;
                }
                IsModified = false;
                BindIndexs(true);
            }
            else
            {
                return;
            }
        }

        public override void refreshBarButton_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            base.refreshBarButton_ItemClick(sender, e);
            Guid currentRowIndex = core.CoreCommon.GetGridFocusRowRefId(gdindexgroup, gridView1, "CorpIndexGroupRefId");
            if (IsModified)//切换指标组的之后如果存在原先指标组的数据已经修改过
            {
                DialogResult result = XtraMessageBox.Show(this, "指标组已有修改，刷新后数据将重置，确定刷新？", "刷新数据", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)//确定刷新
                {
                    InitData();
                }
            }
            else
            {
                InitData();
            };
        }


        private void rcbPoint_Leave(object sender, EventArgs e)
        {
            TextEdit text = sender as TextEdit;
            Guid selectRowIndexRefId = core.CoreCommon.GetGridFocusRowRefId(gdIndexs, gridView1, "CorpIndexRefId");
            CtIndexProcess index = core.CoreIndexEvaluate.OperatorRowIndexs.FirstOrDefault(p => p.CorpIndexRefId == selectRowIndexRefId);
            bool isCorrent = false;
            string point = text.Text.Trim();

            if (point != "")
            {
                //因为控件的值返回的时候始终是"***."，所以后面只能加一个0
                 point =point+"0";
                switch (index.InputTypeId)
                {
                    case (int)IndexPointTypeEnum.TrueOrFalse:
                        if (point == "1.0" || point=="0.0")
                        { 
                            isCorrent = true;
                        }
                        break;
                    case (int)IndexPointTypeEnum.OneToNine:
                        if (Convert.ToDecimal(point) >= 1 && Convert.ToDecimal(point) <= 9 && Convert.ToDecimal(point)%1==0)
                        {
                            isCorrent = true;
                        }
                        break;
                    case (int)IndexPointTypeEnum.ZeroToNine:
                        if (Convert.ToDecimal(point) >= 0 && Convert.ToDecimal(point) <= 9)
                        {
                            isCorrent = true;
                        }
                        break;
                    case (int)IndexPointTypeEnum.percent:
                        if (Convert.ToDecimal(point) >= 0 && Convert.ToDecimal(point) <= 1)
                        {
                            isCorrent = true;
                        }
                        break;
                    default:
                        break;
                }
            }
            else
            {
                index.Score = null;
                isCorrent = true;
            }
            if (!isCorrent)
            {
                DialogResult result = XtraMessageBox.Show(this, "请输入正确的测评值，正确的输入: " + index.Note, "提示");
                text.Focus();
            }
            IsModified = true;
        }

        public override void otherBarButton1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            base.otherBarButton1_ItemClick(sender, e);
            int i = 0;
            foreach (CtIndexProcess item in core.CoreIndexEvaluate.OperatorRowIndexs)
            {
                if (item.Score == null || item.Score.ToString() == "")
                {
                    i++;
                }
            }
            //还有没有填写好的指标
            if (i > 0)
            {
                XtraMessageBox.Show(this, "还有" + i + "个未打分的指标项存在，请确认全部指标已完成后在进行测评。", "指标测评");
                return;
            }
            else {

                  DialogResult result1 = XtraMessageBox.Show(this, "确定开始测评吗？", "指标测评", MessageBoxButtons.OKCancel);
                  if (result1 == DialogResult.OK)//确定保存
                  {
                      FuncResult<decimal> result = core.CoreIndexEvaluate.EvaluateIndexGroup(core.CoreCommon.GetGridFocusRowRefId(gdindexgroup, gridView1, "CorpIndexGroupRefId"));
                      if (result.Status == 0)
                      {
                          XtraMessageBox.Show(this, Language_ch.ResourceManager.GetString(result.Message));
                          return;
                      }
                      else if (result.Status == 1)
                      {
                          XtraMessageBox.Show(this, "测试完毕，此次测评成绩为：" + result.Data.ToString(), "测评成功");
                          InitData();
                      }
                  }
                  else
                  {
                      return;
                  }
                
            }
        }

    }
}
